<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2020 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: https://www.gzdsx.cn
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2020/5/10
 * Time: 1:15 上午
 */

namespace App\Traits\Order;


use App\Models\OrderItem;
use App\Models\Refund;
use App\Support\TradeUtil;
use Illuminate\Http\Request;

trait RefundTrait
{

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function query(Request $request)
    {
        $refund = Refund::with('images')->where('trade_id', $request->input('trade_id'))->firstOrFail();
        return ajaxReturn(['refund' => $refund]);
    }

    public function check(Request $request)
    {
        return ajaxReturn();
    }

    /**
     * 申请退款
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $trade_id = $request->input('trade_id');
        $orderItem = OrderItem::findOrFail($trade_id);
        if (is_null($refund = Refund::where('trade_id', $trade_id)->first())) {
            $refund = new Refund();
            $refund->fill($request->input('refund', []));
            $refund->trade_id = $trade_id;
            $refund->order_id = $orderItem->order_id;
            $refund->refund_no = TradeUtil::createReundNo();
            $refund->receive_state = 1;
            $refund->save();

            foreach ($request->input('images', []) as $image) {
                $refund->images()->create($image);
            }
        }

        if ($order = $orderItem->order) {
            $order->refund_state = 1;
            $order->refund_at = now();
            $order->save();
        }

        $refund->refresh();
        $refund->load(['images']);
        return ajaxReturn(['refund' => $refund]);
    }

    /**
     * 更新退款申请
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request)
    {
        $refund = Refund::findOrFail($request->input('refund_id'));
        if ($refund->refund_state == 1 || $refund->refund_state == 2){
            $attributes = collect($request->input('refund', []));
            $refund->fill($attributes->only([
                'refund_type',
                'refund_amount',
                'refund_reason',
                'refund_desc',
                'receive_state'
            ])->toArray());
            $refund->refund_state = 1;
            $refund->save();

            $refund->images()->delete();
            foreach ($request->input('images', []) as $image) {
                $refund->images()->create($image);
            }
            $refund->refresh();
            $refund->load(['images']);
        }

        return ajaxReturn(['refund' => $refund]);
    }

    /**
     * 取消退款
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function revoke(Request $request)
    {
        $refund = Refund::findOrFail($request->input('refund_id'));
        if ($refund->order) {
            $refund->order->refund_state = 0;
            $refund->order->refund_at = null;
            $refund->order->save();
        }
        $refund->delete();
        return ajaxReturn();
    }

    /**
     * 更新物流
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateShipping(Request $request)
    {
        $refund = Refund::findOrFail($request->input('refund_id'));
        $refund->fill($request->input('shipping', []));
        $refund->refund_state = 4;
        $refund->save();

        return ajaxReturn(['refund' => $refund]);
    }
}
